/**
 * 
 */
package com.antilia.infomaresme.pages.details;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.request.resource.DynamicImageResource;
import org.odlabs.wiquery.core.IWiQueryPlugin;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.core.resources.WiQueryJavaScriptResourceReference;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.services.IImageService;
import com.antilia.infomaresme.data.services.ImageDetail;
import com.antilia.infomaresme.pages.images.ImagePanel;

/**
 * @author Ernesto
 *
 */
public class ImageScrollPanel extends Panel implements IWiQueryPlugin, IHeaderContributor {

	private static final long serialVersionUID = 1L;

	public static WiQueryJavaScriptResourceReference JS = new WiQueryJavaScriptResourceReference(ImageScrollPanel.class, "jquery.tools.js");	

	
	private NonCachingImage selected;
	
	
	/**
	 * @param id
	 */
	public ImageScrollPanel(String id, final List<ImageDetail> imageDetails) {
		super(id);
		GridView<ImageDetail> images = new GridView<ImageDetail>("rows", 
				new ListDataProvider<ImageDetail>(imageDetails)) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final org.apache.wicket.markup.repeater.Item<ImageDetail> item) {
				item.setRenderBodyOnly(true);
				item.add(new ImagePanel("image", item.getModelObject(), 100, 75) {
					
					private static final long serialVersionUID = 1L;

					@Override
					protected void onEvent(AjaxRequestTarget target) {
						NonCachingImage cachingImage = new NonCachingImage("selected", new DynamicImageResource() {
							
							private static final long serialVersionUID = 1L;

							@Override
							protected byte[] getImageData(Attributes attributes) {
								return ImageScrollPanel.this.getImageService().resizeImage(item.getModelObject(), 300, 225).getBytes();
							}
							
				
						});							
						cachingImage.setOutputMarkupId(true);
						selected.replaceWith(cachingImage);
						selected = cachingImage;
						target.add(selected);
					}
				});
			}

			@Override
			protected void populateEmptyItem(org.apache.wicket.markup.repeater.Item<ImageDetail> item) {
				item.setRenderBodyOnly(true);
				item.add(new EmptyPanel("image").setRenderBodyOnly(true));
			}
		};
		images.setColumns(5);
		add(images);
		
		selected = new NonCachingImage("selected", new DynamicImageResource() {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected byte[] getImageData(Attributes attributes) {
				return ImageScrollPanel.this.getImageService().resizeImage(imageDetails.get(0), 300, 225).getBytes();
			}

		});							
		selected.setOutputMarkupId(true);
		add(selected);
	}
	
	@Override
	public void renderHead(IHeaderResponse response) {
		response.renderJavaScriptReference(JS);
	}

	public JsStatement statement() {
		JsStatement jsStatement = new JsStatement();
		return jsStatement.$(null, ".scrollable").chain("scrollable");
	}

	public IImageService getImageService() {
		return InfomaresmeApplication.getQAApplication().getImageService();
	}

}
